<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_员工练习表</title>
</head>
<body>
<input type="text" placeholder="请输入员工姓名" id="i1"><br>
<input type="text" placeholder="请输入员工工资" id="i2"><br>
<input type="text" placeholder="请输入员工岗位" id="i3"><br>
<button onclick="addEmp()">点击添加员工</button>
<hr>
<table border="1px">
    <caption>员工表</caption>
    <tr>
        <th>员工姓名</th>
        <th>员工工资</th>
        <th>员工岗位</th>
    </tr>
</table>
<script>
    //9.定义数组用来保存用户输入的多个员工数据
    let arr=[];
    //1.定义函数，点击按钮时调用
    function addEmp(){
        //2.获取用户的值
        let uName=document.querySelector('#i1').value.trim();
        let uSalary=document.querySelector('#i2').value.trim();
        let uJob=document.querySelector('#i3').value.trim();
        //3.对数据进行校验
        if(!uName||!uSalary||!uJob){
            alert('请输入完整的员工信息！');
            return;
        }
        console.log(uName,uSalary,uJob);
        //4.每多输入一个员工，表格就多1个tr与3个td
        let trE=document.createElement('tr');
        let nameTd=document.createElement('td');
        let salaryTd=document.createElement('td');
        let jobTd=document.createElement('td');
        //5.将收集到的值存入单元格中
        nameTd.innerHTML=uName;
        salaryTd.innerHTML=uSalary;
        jobTd.innerHTML=uJob;
        //6.将单元格存入tr中
        trE.append(nameTd,salaryTd,jobTd);
        console.log(trE,nameTd,salaryTd,jobTd);
        //7.将tr存入table中
        let tableE=document.querySelector('table');
        tableE.append(trE);
        //8.用完清空数据
        document.querySelector('#i1').value='';
        document.querySelector('#i2').value='';
        document.querySelector('#i3').value='';
        //10.将收集到的用户数据存入数组中
        arr.push({
            name:uName,
            salary:uSalary,
            job:uJob
        });
        console.log(arr);
    }
</script>
</body>
</html>